package com.service.calculate;

import com.service.bean.Road_Data;
import com.service.bean.Road_GpsSignal;
import com.service.bean.Road_Vehicle;
import com.service.bean.device.Sensor;
import com.service.bean.point.Point;
import com.service.cache.CacheManager;
import com.service.calculate.vehicle.CalculatorOil;
import com.service.other.Coordinate;
import com.service.other.Logger;
import com.service.socket.protocol.Road_DataProtocol;
import com.service.tools.DateFormat;
import com.service.tools.Tools;

public class Road_SignalCreator {
	
	private Road_Vehicle vehicle = null;
	
	private Road_Data data = null;
	
	private Road_GpsSignal bean = null;
	
	private Road_BasalCalculator basal = null;
	
	private Road_StatusCalculator status = null;
	
	public Road_SignalCreator(String dataArea,String deviceNo,Road_GpsSignal bean,Road_BasalCalculator basal,Road_StatusCalculator status,Road_DataProtocol dp){
		this.bean = bean;	
		this.vehicle = CacheManager.instance().getVehicle_Road(deviceNo);
		// 不同的协议版本对应不同的处理方法
		this.data = dp.Parser(dataArea);
		this.bean.gpsNo = deviceNo;
		this.bean.equipmentId = vehicle.equipmentId;
		this.bean.companyCode = vehicle.company.code;
		this.basal = basal;
		this.status = status;	
	}
	
	/**
	 * 构造函数
	 * @param signal
	 * @param vehicle
	 */
	public Road_SignalCreator(Road_GpsSignal bean,Road_Vehicle vehicle){
		try{
			this.vehicle = vehicle;
			this.bean = bean;
		}catch(Exception e){
			Logger.err(e);
		}
	}
	
	/**
	 * 初步解析, 只解析基础值、状态位、工作状态、经纬度转换和所在区域
	 * @return
	 */
	public Road_GpsSignal createSignal() {
		basal.calculate(data, bean);
		status.calculate(data, bean);
		// this.calculateWorkStatus();
		this.calculateCoordinate();
		// 还没有启动正式的计算，这里为了前端显示，相关字段先暂时使用上一条的数据
		if (vehicle.lastGps != null) {
			bean.oilAmtCal = vehicle.lastGps.oilAmtCal;
			bean.oilAmtCal2 = vehicle.lastGps.oilAmtCal2;
			bean.oilAmtCal3 = vehicle.lastGps.oilAmtCal3;
			bean.todayMile = vehicle.lastGps.todayMile;
			bean.todayMile2 = vehicle.lastGps.todayMile2;
			bean.totalMile = vehicle.lastGps.totalMile;
			bean.totalMile2 = vehicle.lastGps.totalMile2;
		}
		return bean;
	}
	
	/**
	 * 计算非工作状态、油量、里程的变化量
	 * @param signal
	 * @param vehicle
	 */
	public void createSignalInScalarCal(){
		try{
			if("09".equals(vehicle.equipmentKind)){
				this.paverCalculateOilAmt();
			}else{
				this.calculateOilAmt();
			}
			this.calculateMile();
			this.gpsDrift();
		}catch(Exception e){
			Logger.err(e);
		}
	}

	/**
	 * 如果根据时长，距离超过120公里/小时，那么当前的gps坐标取上一条
	 */
	private void gpsDrift() {
		if (vehicle != null && vehicle.lastGps != null) {
			double timeDiff = (bean.gpsTime.getTime() - vehicle.lastGps.gpsTime.getTime()) / 1000.00;
			double distance = Tools.getTwoPointDistance(vehicle.lastGps.baiduLng, vehicle.lastGps.baiduLat,
					bean.baiduLng, bean.baiduLat);
			if (timeDiff > 0) {
				double speed = distance / timeDiff;
				if (speed > 120 * 1000.00 / 3600 && bean.durationtime < 10 * 60) {
					bean.lng = vehicle.lastGps.lng;
					bean.lat = vehicle.lastGps.lat;
					bean.baiduLng = vehicle.lastGps.baiduLng;
					bean.baiduLat = vehicle.lastGps.baiduLat;
					bean.durationtime = vehicle.lastGps.durationtime + timeDiff;
				} else {
					bean.durationtime = 0;
				}
			}
		}
	}

	/**
	 * 油位计算
	 * @param data
	 * @param bean
	 */
	private void calculateOilAmt(){
		// 如果传过来的油位百分比(高度)大于油箱高度，则当前油位等于上一条油位高度
		if (null != vehicle.oilBox && bean.oilHeight > vehicle.oilBox.oilBoxHight) {
			if (null != vehicle.lastGps) {
				bean.oilAmtCal = vehicle.lastGps.oilAmtCal;
				bean.oilAmt = vehicle.lastGps.oilAmt;
			} else {
				bean.oilAmtCal = 0;
				bean.oilAmt = 0;
			}
			return;
		}

		boolean isCal = false;
		if (null != vehicle.oilBox && null != vehicle.oilBox.standards && 0 != vehicle.oilBox.standards.size()) {
			//油量
			double oilAmtCal = CalculatorOil.getOilValue(vehicle.oilBox, bean.oilHeight);
			if (null == vehicle.lastGps) {
				bean.oilAmtCal = oilAmtCal;
				//实际油位百分比 = 油位高度/邮箱高度
				bean.oilAmt = bean.oilHeight*100/vehicle.oilBox.oilBoxHight;
			} else {
				bean.oilAmtCal = (oilAmtCal == 0) ? vehicle.lastGps.oilAmtCal : oilAmtCal;
				//实际油位百分比 = 油位高度/邮箱高度
				bean.oilAmt = (bean.oilHeight == 0) ? vehicle.lastGps.oilAmt : bean.oilHeight*100/vehicle.oilBox.oilBoxHight;
			}
			isCal = true;
		}
		Sensor oilSensor = vehicle.sensorMap.get("10");
		if(oilSensor != null && oilSensor.isOilSensor()){
			if (null != vehicle.oilBox){
				if(!isCal){
					bean.oilAmtCal = getOilNuber(bean.oilHeight, oilSensor);
				}
				bean.oilAmtCal2=getOilNuber(bean.oilAmt2, oilSensor);
				bean.oilAmtCal3=getOilNuber(bean.oilAmt3, oilSensor);
			}
			if(bean.accStatus == 0 && "HIGH".equals(oilSensor.oilMode) && bean.oilHeight==100){
				bean.oilAmtCal =0;
			}
		}else{
			if (null != vehicle.oilBox){
				if(!isCal){
					bean.oilAmtCal = vehicle.oilBox.oilBoxBulk*bean.oilHeight/100.0;
				}
				bean.oilAmtCal2 = vehicle.oilBox.oilBoxBulk*bean.oilAmt2/100.0;
				bean.oilAmtCal3 = vehicle.oilBox.oilBoxBulk*bean.oilAmt3/100.0;
			}
		}
	}
	
	/**
	 * 摊铺机油位计算
	 * @param data
	 * @param bean
	 */
	private void paverCalculateOilAmt(){
		boolean isCal = false;
		if (null != vehicle.oilBox){
			if(!isCal){
				bean.oilAmtCal = vehicle.oilBox.oilBoxBulk*bean.oilAmt/100.0;
			}
			bean.oilAmtCal2 = vehicle.oilBox.oilBoxBulk*bean.oilAmt2/100.0;
			bean.oilAmtCal3 = vehicle.oilBox.oilBoxBulk*bean.oilAmt3/100.0;
		}
	}
	
	/**
	 * 里程变化计算
	 * @param bean
	 */
	private void calculateMile() {
		if (vehicle != null) {
			// 从来没有过定位数据(例如：新安装的车辆接入时)
			if (vehicle.lastGps == null) {
				bean.todayMile = 0.00;
				bean.todayMile2 = 0.00;
				bean.totalMile = vehicle.totalMile;
				bean.totalMile2 = vehicle.totalMile2;
//				vehicle.lastGps = new GpsSignal(vehicle.device.deviceNo);
			} else {
				// 如果最后一条定位数据时间与当前GPS时间是同一天
				if (vehicle.lastGps.gpsTime != null && DateFormat.format(vehicle.lastGps.gpsTime, "yyyy-MM-dd").equals(DateFormat.format(bean.gpsTime, "yyyy-MM-dd"))) {
					// 如果当前是非定位数据，则使用上一条GPS数据的里程信息
					if (bean.gpsStatus==0) {
						bean.todayMile = vehicle.lastGps.todayMile;
						bean.todayMile2 = vehicle.lastGps.todayMile2;
						bean.totalMile = vehicle.lastGps.totalMile;
						bean.totalMile2 = vehicle.lastGps.totalMile2;
					} else { // 如果当前是定位数据，则使用计算逻辑
						double mileChange = bean.curMile - vehicle.lastGps.curMile;
						double mileChange2 = bean.curMile2 - vehicle.lastGps.curMile2;
						if (mileChange >= 0) {
							bean.todayMile = vehicle.lastGps.todayMile + mileChange;
							bean.totalMile = vehicle.lastGps.totalMile + mileChange;
						} else {
							bean.todayMile = vehicle.lastGps.todayMile;
							bean.totalMile = vehicle.lastGps.totalMile;
						}
						if (mileChange2 >= 0) {
							bean.todayMile2 = vehicle.lastGps.todayMile2 + mileChange2;
							bean.totalMile2 = vehicle.lastGps.totalMile2 + mileChange2;
						} else {
							bean.todayMile2 = vehicle.lastGps.todayMile2;
							bean.totalMile2 = vehicle.lastGps.totalMile2;
						}
					}
				} else { // 如果最后一条定位数据时间与当前GPS时间不是同一天(例如：今天的第一条GPS)
					bean.todayMile = 0.00;
					bean.todayMile2 = 0.00;
					bean.totalMile = vehicle.lastGps.totalMile;
					bean.totalMile2 = vehicle.lastGps.totalMile2;
				}
			}
			// 计算完成后，更新vehicle.lastGps
			//vehicle.lastGps = bean
		}
	}
	
	private double getOilNuber(double oilAmt,Sensor oilSensor){
	  	double oil=0;
		oil=(oilSensor.para1*oilAmt*oilAmt*oilAmt+oilSensor.para2*oilAmt*oilAmt+oilSensor.para3*oilAmt+oilSensor.para4)/100*vehicle.oilBox.oilBoxBulk;
		return oil;
	}
	
	/**
	 * gps坐标转百度坐标
	 */
	private void calculateCoordinate(){
		Point p = new Point(bean.lat,bean.lng);
		p = Coordinate.gpsToWgs84(p);
		p = Coordinate.wgs84ToBd09(p);
		bean.baiduLat = p.baiduLat;
		bean.baiduLng = p.baiduLng;
		bean.isPosition = 1;
	}

}
